<template>
  <div class="login-wrapper">
    <!-- 江湖风格背景元素 -->
    <div class="jianghu-background">
      <div class="mountain mountain-1"></div>
      <div class="mountain mountain-2"></div>
      <div class="mountain mountain-3"></div>
      <!-- 月亮 -->
      <div class="moon"></div>
      <!-- 云彩 -->
      <div class="cloud cloud-1"></div>
      <div class="cloud cloud-2"></div>
      <div class="cloud cloud-3"></div>
      <!-- 飞鸟 -->
      <div class="bird bird-1"></div>
      <div class="bird bird-2"></div>
      <div class="bird bird-3"></div>
      <!-- 江湖元素 -->
      <div class="jianghu-element element-1"> swords </div>
      <div class="jianghu-element element-2"> mountain </div>
      <div class="jianghu-element element-3"> river </div>
    </div>

    <div class="login-container">
      <el-card class="login-card jianghu-card">
        <div class="login-header">
          <div class="logo-section">
            <div class="logo-icon">🗡️</div>
          </div>
          <h2 class="login-title">江湖暗号系统</h2>
          <p class="login-subtitle">天王盖地虎，宝塔镇河妖</p>
        </div>

        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          @submit.prevent="handleLogin"
        >
          <el-form-item label="江湖暗号">
            <el-input
              v-model="loginForm.question"
              placeholder="请输入江湖暗号"
              prefix-icon="QuestionFilled"
              size="large"
              disabled
            />
          </el-form-item>

          <el-form-item prop="answer" label="你的答案">
            <el-input
              v-model="loginForm.answer"
              placeholder="请输入你的答案"
              prefix-icon="Lock"
              size="large"
              show-password
              @input="(value) => loginForm.answer = value"
              clearable
            />
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              size="large"
              class="login-btn jianghu-btn"
              :loading="loading"
              native-type="submit"
            >
              🚪 江湖接头
            </el-button>
          </el-form-item>
          
          <div class="hint-section" v-if="showHint">
            <el-alert
              title="💡 江湖提示"
              description="行走江湖，没有暗号寸步难行，说出你的答案吧！"
              type="info"
              show-icon
            />
          </div>
        </el-form>

        <div class="login-footer">
          <p class="hint-text">传言：答案就在五个数字之间...</p>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

const loginForm = ref({
  question: '天王盖地虎',
  answer: ''
})

const loginRules = ref({
  answer: [
    { required: true, message: '请输入暗号答案', trigger: 'blur' }
  ]
})

const loading = ref(false)
const showHint = ref(false)
const loginFormRef = ref(null)

const handleLogin = () => {
  if (!loginFormRef.value) {
    console.error('表单引用未初始化')
    return
  }

  loginFormRef.value.validate((valid) => {
    if (!valid) {
      ElMessage.warning('请输入暗号答案')
      showHint.value = true
      return
    }

    loading.value = true

    // 检查答案是否正确
    if (loginForm.value.answer === '12345') {
      // 答案正确，模拟登录成功
      const token = 'mock-token-' + Date.now()
      localStorage.setItem('token', token)
      
      // 处理登录后重定向
      const redirect = router.currentRoute.value.query.redirect || '/'
      console.log('登录成功，准备跳转到:', redirect, 'at:', new Date().toISOString())
      
      // 使用location.href强制跳转，避免Vue Router的异步问题
      window.location.href = redirect
      loading.value = false
    } else {
      // 答案错误
      ElMessage.error('暗号错误！你可能不是我们要找的人...')
      loading.value = false
      showHint.value = true
    }
  })
}
</script>

<style scoped>
.login-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: linear-gradient(135deg, #0c1427 0%, #1a2a4f 100%);
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  position: relative;
}

/* 江湖风格背景 */
.jianghu-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 0;
}

/* 山脉 */
.mountain {
  position: absolute;
  bottom: 0;
  border-left: 150px solid transparent;
  border-right: 150px solid transparent;
  border-bottom: 200px solid #0a1020;
}

.mountain-1 {
  left: 10%;
  border-bottom-color: #080d1a;
  height: 250px;
  width: 300px;
}

.mountain-2 {
  right: 15%;
  border-bottom-color: #0a1222;
  height: 220px;
  width: 280px;
}

.mountain-3 {
  left: 40%;
  border-bottom-color: #060a15;
  height: 180px;
  width: 250px;
}

/* 月亮 */
.moon {
  position: absolute;
  top: 80px;
  right: 120px;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: radial-gradient(circle, #f5e4b4 0%, #f0d492 70%, #e6c07a 100%);
  box-shadow: 0 0 40px #f5e4b4, 0 0 80px rgba(245, 228, 180, 0.5);
  animation: moonGlow 4s ease-in-out infinite alternate;
}

/* 云朵 */
.cloud {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  filter: blur(20px);
}

.cloud-1 {
  top: 120px;
  left: 20%;
  width: 100px;
  height: 40px;
  animation: cloudFloat 25s linear infinite;
}

.cloud-2 {
  top: 200px;
  right: 25%;
  width: 150px;
  height: 50px;
  animation: cloudFloat 30s linear infinite reverse;
}

.cloud-3 {
  top: 300px;
  left: 10%;
  width: 80px;
  height: 30px;
  animation: cloudFloat 20s linear infinite;
}

/* 飞鸟 */
.bird {
  position: absolute;
  width: 20px;
  height: 10px;
  background: rgba(255, 255, 255, 0.7);
  border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
}

.bird-1 {
  top: 15%;
  left: -5%;
  animation: fly 20s 0s infinite linear;
}

.bird-2 {
  top: 20%;
  left: -10%;
  animation: fly 25s 5s infinite linear;
}

.bird-3 {
  top: 10%;
  left: -15%;
  animation: fly 30s 10s infinite linear;
}

/* 江湖元素文字 */
.jianghu-element {
  position: absolute;
  color: rgba(255, 255, 255, 0.05);
  font-size: 24px;
  font-weight: bold;
  text-transform: uppercase;
  pointer-events: none;
}

.element-1 {
  top: 30%;
  left: 5%;
  transform: rotate(-20deg);
}

.element-2 {
  top: 60%;
  right: 10%;
  transform: rotate(15deg);
}

.element-3 {
  top: 40%;
  left: 70%;
  transform: rotate(5deg);
}

/* 登录卡片 */
.jianghu-card {
  width: 450px;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  background: rgba(25, 35, 60, 0.85);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(245, 228, 180, 0.3);
  position: relative;
  z-index: 1;
  color: #f0e6d2;
  transform: translateY(0);
  transition: transform 0.3s ease;
}

.jianghu-card:hover {
  transform: translateY(-5px);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-section {
  display: flex;
  justify-content: center;
  margin-bottom: 15px;
}

.logo-icon {
  font-size: 48px;
  line-height: 1;
}

.login-title {
  color: #f5e4b4;
  margin-bottom: 12px;
  font-size: 28px;
  font-weight: bold;
  text-shadow: 0 0 10px rgba(245, 228, 180, 0.5);
}

.login-subtitle {
  color: #c0b090;
  margin: 0;
  font-size: 16px;
}

:deep(.el-form-item__label) {
  color: #d4c8a8 !important;
}

:deep(.el-input__wrapper) {
  background: rgba(15, 25, 45, 0.7) !important;
  border: 1px solid rgba(245, 228, 180, 0.2) !important;
  border-radius: 6px !important;
  box-shadow: none !important;
}

:deep(.el-input__inner) {
  color: #f0e6d2 !important;
  caret-color: #f5e4b4 !important;
}

:deep(.el-input__inner)::placeholder {
  color: #8a7f66 !important;
}

.jianghu-btn {
  width: 100%;
  margin-top: 10px;
  background: linear-gradient(45deg, #cdaa52, #b8943d);
  border: none;
  font-weight: bold;
  letter-spacing: 2px;
  color: #0c1427;
  box-shadow: 0 0 15px rgba(205, 170, 82, 0.5);
  transition: all 0.3s ease;
  border-radius: 6px;
}

.jianghu-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 20px rgba(205, 170, 82, 0.7);
}

.hint-section {
  margin-top: 20px;
}

:deep(.el-alert) {
  background: rgba(40, 60, 100, 0.6) !important;
  border: 1px solid rgba(245, 228, 180, 0.2) !important;
  border-radius: 6px !important;
}

:deep(.el-alert__title) {
  color: #f5e4b4 !important;
  font-weight: bold !important;
}

:deep(.el-alert__description) {
  color: #c0b090 !important;
}

.hint-text {
  text-align: center;
  color: #8a7f66;
  font-size: 14px;
  margin: 0;
}

/* 动画 */
@keyframes moonGlow {
  0% {
    box-shadow: 0 0 30px #f5e4b4, 0 0 60px rgba(245, 228, 180, 0.5);
  }
  100% {
    box-shadow: 0 0 50px #f5e4b4, 0 0 100px rgba(245, 228, 180, 0.7);
  }
}

@keyframes cloudFloat {
  0% {
    transform: translateX(-100px);
  }
  100% {
    transform: translateX(calc(100vw + 100px));
  }
}

@keyframes fly {
  0% {
    transform: translateX(0) translateY(0);
  }
  25% {
    transform: translateX(25vw) translateY(20px);
  }
  50% {
    transform: translateX(50vw) translateY(0);
  }
  75% {
    transform: translateX(75vw) translateY(-20px);
  }
  100% {
    transform: translateX(100vw) translateY(0);
  }
}

@media (max-width: 576px) {
  .jianghu-card {
    width: 90%;
    padding: 30px 20px;
    border-radius: 10px;
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .mountain {
    display: none;
  }
  
  .moon {
    top: 40px;
    right: 40px;
    width: 60px;
    height: 60px;
  }
  
  .jianghu-btn {
    border-radius: 5px;
  }
}
</style>